<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of thanhvien
 *
 * @author Bui
 */
include_once (dirname(__FILE__) . "/thanhvien_diachi.php");

class Thanhvien extends Thanhvien_diachi {
    private $popup=0;
    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->library('form_validation');
        $this->load->helper('recaptchalib');
        $this->load->helper('form');
        $this->load->model('thanhvien_model', '', TRUE);
    }
    
    public function thongbaogiasu() {
        $this->load->model('thongbao_model');
        $session_data = $this->session->userdata('logged_in');
        $ketqua = $this->thongbao_model->load_thongbaogiasu($session_data['id']);
        $id = array_pop($ketqua);
        $i = 0;
        $giasu_id = NULL;
        if($ketqua != NULL){
        foreach ($id as $value1) {
            $j = 0;
            foreach ($ketqua as $value2) {
                 if($value2['LamGiaSu_id'] == $value1)
                    $j++;
            }
            $giasu_id[$i]['id'] = $value1;
            $giasu_id[$i]['count'] = $j;
            $i++;
        }
        }
        $data['data'] = $ketqua;
        $data['giasu_id'] = $giasu_id;
//        $this->load->view('user/common/header_view');
//        $this->load->view('user/thanhvien/thongbaogiasu_view.php', $data);
//        $this->load->view('user/common/footer_view');
        return $data;
    }
    
    

    public function thongbaolop() {
        $this->load->model('thongbao_model');
        $session_data = $this->session->userdata('logged_in');
        $ketqua = $this->thongbao_model->load_thongbaolop($session_data['id']);
        $id = array_pop($ketqua);
        $i = 0;
        $lop_id = NULL;
        if($ketqua != NULL){
        foreach ($id as $value1) {
            $j = 0;
            foreach ($ketqua as $value2) {
                 if($value2['TimGiaSu_id'] == $value1)
                    $j++;
            }
            $lop_id[$i]['id'] = $value1;
            $lop_id[$i]['count'] = $j;
            $i++;
        }
        }
        $data['data'] = $ketqua;
        $data['lop_id'] = $lop_id;
        
//        $this->load->view('user/common/header_view');
//        $this->load->view('user/thanhvien/thongbaolop_view.php', $data);
//        $this->load->view('user/common/footer_view');
        return $data;
    }

    public function display_hoatdong_timgiasu() {
        $this->load->model('display_model');
        $session_data = $this->session->userdata('logged_in');
        $data = $this->display_model->load_hoatdong_timgiasu($session_data['id']);
        return $data;
    }

    public function display_hoatdong_lamgiasu() {
        $this->load->model('display_model');
        $session_data = $this->session->userdata('logged_in');
        $data = $this->display_model->load_hoatdong_lamgiasu($session_data['id']);
//        echo '<pre>';
//        print_r($data);
//        echo '</pre>';
        return $data;
    }

    public function no_login() {
        $this->load->helper(array('form'));
        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
        if ($this->form_validation->run($this) == FALSE) {
            $this->load_html_2('user/thanhvien/signin_view', 'user/thanhvien/no_signin', $data = '');
        } else {
            $this->home();
        }
    }

    public function login() {
        $this->load->helper(array('form'));
        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
        if ($this->form_validation->run($this) == FALSE) {
                $popup=$this->session->userdata('popup');
                $path='';
                
                switch ($popup) {
                    case 1:
                        $path='user/thanhvien/popup_notlogin';
                        break;
                    case 2:
                        $path='user/thanhvien/popup_suggest_reg';
                        break;
                }
                
                if ($path=='') $this->load_html('user/thanhvien/signin_view', $data='');
                else $this->load_html_2('user/thanhvien/signin_view',$path, $data=''); 
                
                $this->session->set_userdata('popup', 0);
        } else {
            $this->home();
        }
    }

    public function check_database($password) {
        $username = $this->input->post('username');
        $result = $this->thanhvien_model->login($username, $password);
        if ($result) {
            $sess_array = array();
            foreach ($result as $row) {
                $sess_array = array(
                    'id' => $row->id,
                    'sign_in' => TRUE,
                );
                $this->session->set_userdata('logged_in', $sess_array);
            }
            return TRUE;
        } else {
            $this->form_validation->set_message('check_database', 'Invalid username or password');
            return false;
        }
    }

    function registration() {
        $data = array();
        $data['error'] = NULL;
        $data['publickey'] = '6Lc9Z-8SAAAAAOa298dF4Lw1EG1-f7xmoB2ij-T6';
        $this->load_html('user/thanhvien/registration_view', $data);
    }
    function hub_registration(){
        $data = array();
        $this->load_html('user/thanhvien/hub_registration_view', $data);
    }   
    
    public function submit_registration() {
        if($this->input->post() )
        {
        // field name, error message, validation rule
//        $this->form_validation->set_rules('username', 'Tài khoản', 'trim|required|min_length[4]|max_length[20]|is_unique[gs7s_thanhvien.username]|xss_clean');
        $this->form_validation->set_rules('username', 'Tài khoản', 'trim|required|min_length[4]|is_unique[gs7s_thanhvien.username]|max_length[20]|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]');
        $this->form_validation->set_rules('con_password', 'Xác nhận password', 'trim|required|matches[password]');
//        $this->form_validation->set_rules('email_address', 'Email', 'trim|required|valid_email|is_unique[gs7s_thanhvien.email]');
        $this->form_validation->set_rules('email_address', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('con_email', 'Xác nhận email', 'trim|required|matches[email_address]');
        $this->form_validation->set_rules('sexual','gioitinh','trim|required|xss_clean');
        $recaptcha_response_field = $this->input->post('recaptcha_response_field');
        $this->form_validation->set_rules('recaptcha_challenge_field', 'Captcha', 'trim|required|callback_check_captcha[' . $recaptcha_response_field . ']');
            if ($this->form_validation->run() == FALSE) {
            $this->registration();
               } else {
            $this->thanhvien_model->add_user();  
            //TO DO: gui email kich hoat
            $data['username'] = $this->input->post('username');
            $data['password'] = $this->input->post('password');
            $data['email'] = $this->input->post('email_address');
            $data["domain"] = anchor($this->config->item("base_url"),$this->config->item("base_url"));
            $data["active_link"] = anchor($this->config->item("base_url") . '/index.php/' .
                    "thanhvien/thanhvien/activeUser/" . $this->input->post('username'));
            $data["cancel_link"] = anchor($this->config->item("base_url") . '/index.php/' .
                    "thanhvien/thanhvien/deactiveUser/" . $this->input->post('username'));
            $this->load->library('../controllers/ltt_email_c');
            $activew = new $this->ltt_email_c();
            $activew->set_data($data);
            $activew->send_active();
           redirect('/thanhvien/thanhvien/thank_for_regis','refresh');
//            echo "sent";
            //------------------
               }
        }      
    }

      function thank_for_regis() {
        $data = array();
        $this->load_html('user/thanhvien/thanhvien_thank', $data);
    }

    function self_thank_for_register() {
        $this->load_html('user/thanhvien/self_thanhvien_thank_view', $data = '');
    }

    public function check_captcha($recaptcha_challenge_field, $recaptcha_response_field) {
        $privatekey = '6Lc9Z-8SAAAAAHB8jzEFqHdCLPWr7qlHj8vI98LZ';
        $resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
        if (!$resp->is_valid) {
            $this->form_validation->set_message('check_captcha', 'Invalid %s.');
            return false;
        } else {
            return true;
        }
    }

    function activeUser($id) {
        $this->thanhvien_model->active($id);
        echo 'Actived';
    }

    function deactiveUser($id) {
        $this->thanhvien_model->deactive($id);
        echo 'Deactived';
    }

    function forgot_pass() {
        $email = $this->input->post('email');
        $select = "id,password"; //tuong duong $this->db->select('id,password');
        $where = array(//tuong duong $this->db->where('email',$email);
            'key' => 'email',
            'value' => $email,
        );
        $this->load->model('thanhvien_model');
        $result = $this->thanhvien_model->load_thanhvien($select, $where);

        //kiem tra Email co ton tai hay khong
        if ($result == FALSE) {
            $data['result'] = 'Email khong ton tai!';
        } else {
            //goi mail

            $data['result'] = 'Mat khau moi da duoc goi vao email';
        }

        $data['return'] = "thanhvien/thanhvien/home"; //dia chi quay lai cua nut back
        $this->load->view('user/thanhvien/notsubmit', $data);
    }

    function load_forgot_pass() {
        $this->load_html('user/thanhvien/forgot_pass_view', $data = '');
    }

    function home() {
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $check_map = $this->check_id_exists($session_data['id']);
            if ($check_map == TRUE) {
                $data = array(
                    'check_map' => $check_map,
                    'query' => $this->thanhvien_model->get_user($session_data['id']),
                    'map' => $this->load_map_2($session_data['id']),
                    'map_gs' => $this->load_map($session_data['id']),
                    'diachi' => $this->load_diachi($session_data['id']),
                    'thongbao_giasu' => $this->thongbaogiasu(),
                    'thongbao_lop' => $this->thongbaolop(),
                    'hoatdong_giasu' => $this->display_hoatdong_lamgiasu(),
                    'hoatdong_phuhuynh' => $this->display_hoatdong_timgiasu(),
                );
            } else {
                $data = array(
                    'check_map' => $check_map,
                    'query' => $this->thanhvien_model->get_user($session_data['id']),
                    'map' => $this->load_map($session_data['id']),
                    'diachi' => $this->load_diachi($session_data['id']),
                    'thongbao_giasu' => $this->thongbaogiasu(),
                    'thongbao_lop' => $this->thongbaolop(),
                    'hoatdong_giasu' => $this->display_hoatdong_lamgiasu(),
                    'hoatdong_phuhuynh' => $this->display_hoatdong_timgiasu(),
                );
            }
            
            $this->load_html('user/thanhvien/profile', $data);
        } else {
            redirect('login', 'refresh');
        }
    }

    function loadProfile($id) {
        $data['query'] = $this->thanhvien_model->get_user($id);
        $this->load_html('user/thanhvien/update_profile', $data);
    }
    
    

    function load_self_Profile($mahoa_id) {
        $id = base_convert($mahoa_id, 3, 10);
        
        $data['query'] = $this->thanhvien_model->get_user($id);
        $this->load_html('user/thanhvien/self_signin_view', $data);
    }

    function test_self_Profile($id) {
        $this->form_validation->set_rules('username', 'Tài khoản', 'trim|required|min_length[4]|max_length[20]|xss_clean|is_unique[gs7s_thanhvien.username]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]');
        $this->form_validation->set_rules('con_password', 'Xác nhận password', 'trim|required|matches[password]');
        if ($this->form_validation->run() == FALSE) {
            $this->load_self_Profile($id);
        } else {
            $this->update_selfProfile($id);
        }
    }

    function update_selfProfile($id) {
        $data = array(
            'username' => $this->input->post('username'),
            'password' => md5($this->input->post('password')),
        );
        $this->thanhvien_model->update_user($id, $data);
        $this->home();
    }

    function updateProfile($id) {
        $data = array(
            'tenhienthi' => $this->input->post('display_name'),
            'sodienthoai' => $this->input->post('phone_number'),
            'gioitinh' => $this->input->post('gioitinh'),
            'diachi' => $this->input->post('address')
        );
        $this->thanhvien_model->update_user($id, $data);
        $this->home();
    }
    
    
    function logout() {
        $this->session->unset_userdata('logged_in');
        redirect('thanhvien/thanhvien/registration', 'refresh');
        session_destroy();
    }

    function upgradeProfile($id) {
        $data = array(
            'is7s' => '1',
        );
        $this->thanhvien_model->update_user($id, $data);
        redirect('thanhvien/thanhvien/home', 'refresh');
    }

    function load_html($path, $data) {
        $this->load->view('user/common/header_view', $data);
        $this->load->view('user/common/left_sidebar_view', $data);
        $this->load->view('user/common/head_content_view', $data);
        $this->load->view($path, $data);
        $this->load->view('user/common/right_sidebar_view', $data);
        $this->load->view('user/common/footer_view', $data);
    }

    function load_html_2($path, $path2, $data) {
        $this->load->view('user/common/header_view', $data);
        $this->load->view('user/common/left_sidebar_view', $data);
        $this->load->view('user/common/head_content_view', $data);
        $this->load->view($path2, $data);
        $this->load->view($path, $data);
        $this->load->view('user/common/right_sidebar_view', $data);
        $this->load->view('user/common/footer_view', $data);
    }

    function self_registration() {
        $data = array();
        $data['error'] = NULL;
        $data['publickey'] = '6Lc9Z-8SAAAAAOa298dF4Lw1EG1-f7xmoB2ij-T6';
        $this->load_html('user/thanhvien/self_registration_view', $data);
    }

    public function submit_self_reg() {
        $this->load->helper('string');
        $this->form_validation->set_rules('email_address', 'Email', 'trim|required|valid_email|is_unique[gs7s_thanhvien.email]');
        
        if ($this->form_validation->run() == FALSE) {
            $this->self_registration();
        } else {
            //TO DO: gui email xac nhan
            $user = random_string('alnum', 12);
            $pass = random_string('alnum', 12);

            $data['username'] = $user;
            $data['password'] = $pass;
            
            $data['email'] = $this->input->post('email_address');

            $this->thanhvien_model->self_add_user($data);
            $load_id = $this->thanhvien_model->self_load_id($data['email']);
            $mahoa_id = base_convert($load_id, 10, 3);
            $data["domain"] = anchor($this->config->item("base_url"), $this->config->item("base_url"));
            $data["active_link"] = anchor($this->config->item("base_url") . '/index.php/' . "thanhvien/thanhvien/load_self_Profile/" . $mahoa_id, 'mời bạn kích vào link!');

            $this->load->library('../controllers/ltt_email_c');
            $activew = new $this->ltt_email_c();
            $activew->set_data($data);
            $activew->send_confirm();

            $this->self_thank_for_register();
        }
    }
    public function plogin() {
        if ($this->input->post('submit'))
        {
            $this->session->set_userdata('popup', $this->input->post('submit'));
            redirect('login', 'refresh');
        }
    }
}

?>
